package com.itboye.mengwu.app.ui.mymeng;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.MyRunnable;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.core.utils.XImageLoader.DownloadImageFromNetWork;
import com.itboye.mengwu.R;
import com.itboye.mengwu.R.id;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.adapter.YuErJingCommentDetailAdapter;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.aizhiwu.PingLunAndHuiFuDetailActivity;
import com.itboye.mengwu.chatemoji.FaceInputView;
import com.itboye.mengwu.chatemoji.FaceInputView.Face;
import com.itboye.mengwu.chatemoji.FaceInputView.OnFaceClickListener;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.itboye.mengwu.main.MainActivity;
import com.itboye.sunsun.volley.Spanned2String;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyTieZiDetailActivity extends BaseActivity implements Observer {
	private MyListView list_wodedongtaidetail;
	private ImageView back;// 返回按钮
	TextView txt_title, author_name;
	ImageView img_other, img_dianzan;
	TextView baby_years, baby_sex, dianzan_num, pinglun_num;
	private TextView guanzhu;// 关注按钮
	private TextView question_show;// 问题
	private ImageView author_headewrshow;// 头像
	private TextView hot_question_logoView;// 热门标志
	private TextView mom_answer_nameView;// 妈妈提问用户头像
	private TextView answer_headewrshowView;// 回答用户头像
	private TextView answer_nameView;// 回答用户名字
	TextView answer_textView;// 回答内容
	TieZiBaoHanReply.GetAList model;
	LinearLayout image_container;
	EditText edt_comments;
	Button btn_send;
	TextView huifu_tv;
	TieZiPresenter presenter;
	View view2;
	boolean isclick = true;
	private String tid;
	private String uid;
	String babyyears;
	String babysex;
	private FaceInputView faceInputView;
	private LinearLayout faceInputContainer;
	private ImageView emoji;
	protected boolean isShowEmotion;
	String type = "回复";
	int position, chidPosition;
	LinearLayout huifu;
	ImageView img_pinglun;
	DisplayMetrics displayMetrics = new DisplayMetrics();
	String IScollection;

	// ArrayList<TieZiBaoHanReply.GetAList> aLists = new
	// ArrayList<TieZiBaoHanReply.GetAList>();
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.mom_answer;
	}

	@Override
	public void onMyCreate() {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
				.build());

		showProgressDialog("数据加载中，请稍后", true);
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		presenter = new TieZiPresenter(this);
		tid = getIntent().getStringExtra("tid");
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		presenter.zhutiXiangqing(uid, tid, "3");
		faceInputView = new FaceInputView(this);
		faceInputContainer.addView(faceInputView);
		faceInputView.setOnClickListener(new FaceInputListenr());
		emoji.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isShowEmotion) {
					faceInputContainer.setVisibility(View.VISIBLE);
				} else {
					faceInputContainer.setVisibility(View.GONE);
				}
				isShowEmotion = !isShowEmotion;
				((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(MyTieZiDetailActivity.this
								.getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);

			}
		});
		edt_comments.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isShowEmotion) {
					faceInputContainer.setVisibility(View.VISIBLE);
				} else {
					faceInputContainer.setVisibility(View.GONE);
				}
				isShowEmotion = !isShowEmotion;
			}
		});
		img_pinglun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_comments.setHint("回复 " + model.getAuthor() + ":");
				type = "回复";
				huifu.setVisibility(View.VISIBLE);
			}
		});
		img_dianzan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (judgeLogin()) {
					String isFav = (String) v.getTag();
					if (isFav.equals("0")) {
						presenter.dianzan(uid, "1", tid);
					} else {
						presenter.dianzan(uid, "0", tid);
					}
				}
			}
		});
		initview();
		initevents();
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public boolean judgeLogin() {
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		if (uid == null || uid.equals("")) {

			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (faceInputContainer.getVisibility() == View.VISIBLE) {
				faceInputContainer.setVisibility(View.GONE);
				isShowEmotion = false;
				return false;
			} else {
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private class FaceInputListenr implements OnFaceClickListener {

		@Override
		public void selectedFace(Face face) {
			int id = face.faceId;
			if (id == R.drawable.ic_face_delete_normal)// 删除按钮
			{
				int index = edt_comments.getSelectionStart();
				if (index == 0)
					return;
				Editable editable = edt_comments.getText();
				editable.delete(index - 1, index);// 删除最后一个字符或表情
			} else {
				edt_comments.append(Html.fromHtml("<img src='" + id + "'/>",
						imageGetter, null));// 添加表情
			}
		}

	}

	// 输入表情需要
	ImageGetter imageGetter = new ImageGetter() {
		@Override
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			@SuppressWarnings("deprecation")
			Drawable d = getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};
	protected String toUid;
	protected View popup;
	protected PopupWindow popupWindow;

	private void initevents() {
		// TODO Auto-generated method stub

		list_wodedongtaidetail
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							final int arg2, long arg3) {
						position = arg2;
						// TODO Auto-generated method stub
						// Intent intent = new Intent(
						// YuErJingMomDetailActivity.this,
						// EachAnswerActivity.class);
						// startActivity(intent);
						// finish();
						toUid = model.getReplyInfo().getList().get(arg2)
								.getAuthorId();
						System.out.println(toUid + ":" + uid);
						if (uid.equals(toUid)) {
							final Dialog customDialog = new Dialog(
									MyTieZiDetailActivity.this, R.style.dialog);
							LayoutInflater inflater = MyTieZiDetailActivity.this
									.getLayoutInflater();
							View mView = inflater
									.inflate(
											R.layout.dialog_delete_replyorcomment,
											null);
							TextView delete = (TextView) mView
									.findViewById(R.id.delete);
							TextView cancel = (TextView) mView
									.findViewById(R.id.cancel);
							// LayoutParams params=new
							// LayoutParams(getActivity().getWindowManager().getDefaultDisplay().getWidth(),100);
							// mView.setLayoutParams(params);
							customDialog
									.requestWindowFeature(Window.FEATURE_NO_TITLE);
							customDialog.setContentView(mView);

							customDialog.show();
							delete.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									presenter.deletePingLun(toUid,
											model.getFid(), tid, model

											.getReplyInfo().getList().get(arg2)
													.getPid());// 刪除回復
									customDialog.dismiss();
								}
							});
							cancel.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									customDialog.dismiss();
								}
							});
						} else {
							type = "回复评论";
							edt_comments.setHint("回复 "
									+ model.getReplyInfo().getList().get(arg2)
											.getAuthor() + ":");
							((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
									.showSoftInput(edt_comments, 0);
							huifu.setVisibility(View.VISIBLE);
						}
					}
				});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		btn_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (judgeLogin()) {
					if (edt_comments.getText().toString().trim() == null) {
						ByAlert.alert("请输入内容");
						return;
					}
					if (!type.equals("回复评论")) {
						presenter.pinglun(
								// 回复评论
								model.getFid(),
								model.getTid(),
								SPUtils.get(MyTieZiDetailActivity.this, null,
										SpPublicParams.UID, "") + "",
								Spanned2String.parse(edt_comments.getText()),
								null, "");
						huifu.setVisibility(View.GONE);
					} else {// String fid, String pid, String tid,String
							// authorId,
							// String comment, String toUid
						presenter.huiFuPingLun(model.getFid(), model
								.getReplyInfo().getList().get(position)
								.getPid(),
								model.getReplyInfo().getList().get(position)
										.getTid(), uid,
								Spanned2String.parse(edt_comments.getText()),
								model.getReplyInfo().getList().get(position)
										.getAuthorId());
					}
					// ((InputMethodManager)
					// getSystemService(INPUT_METHOD_SERVICE))
					// .hideSoftInputFromWindow(
					// YuErJingMomDetailActivity.this
					// .getCurrentFocus().getWindowToken(),
					// InputMethodManager.HIDE_NOT_ALWAYS);
					huifu.setVisibility(View.GONE);
					type = "回复";
				}
			}
		});
	}

	private void initview() {
		// TODO Auto-generated method stub
		answer_headewrshowView = (TextView) findViewById(R.id.answer_answer_headewrshow);
		answer_nameView = (TextView) findViewById(R.id.answer_answer_name);
		txt_title.setText("我的发布详情");
		guanzhu.setText("删除");
		img_other.setBackgroundResource(R.drawable.gengduo);
		// guanzhu.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// String focusValue = v.getTag().toString();
		// if (judgeLogin()) {
		// if (focusValue.equals("0")) {
		// presenter.guanzhu(uid, toUid, "1");
		// } else {
		// presenter.guanzhu(uid, toUid, "0");
		// }
		// }
		// }
		// });
		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popup = LayoutInflater.from(MyTieZiDetailActivity.this)
						.inflate(R.layout.popup_more, null);
				TextView collection = (TextView) popup
						.findViewById(R.id.collection);
				TextView homepage = (TextView) popup
						.findViewById(R.id.homepage);
				collection.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("hu--collectionValue", IScollection);
						if (IScollection.equals("0")) {
							presenter.favorite(tid, uid, "1");
							ByAlert.alert("收藏成功");
						} else {
							presenter.favorite(tid, uid, "0");
						}
						popupWindow.dismiss();
					}
				});
				homepage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Intent intent = new
						// Intent(MyTieZiDetailActivity.this,
						// MainActivity.class);
						// startActivity(intent);
						// popupWindow.dismiss();
						// finish();
						ByAlert.alert("自己不可以举报自己哦");
					}
				});
				popupWindow = new PopupWindow(popup, 200,
						LayoutParams.WRAP_CONTENT);
				popupWindow.setOutsideTouchable(true);
				popupWindow.setBackgroundDrawable(new BitmapDrawable());
				popupWindow.showAsDropDown(v);
			}
		});
	}

	OnClickListener huiFuPingLunOnclickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			// ByAlert.alert("dianjihuiu" + v.getTag());
			position = Integer.parseInt(v.getTag(R.id.tag_first) + "");
			chidPosition = Integer.parseInt(v.getTag(R.id.tag_second) + "");
			if (v.getTag().toString().equals(uid)) {
				final Dialog customDialog = new Dialog(
						MyTieZiDetailActivity.this, R.style.dialog);
				LayoutInflater inflater = MyTieZiDetailActivity.this
						.getLayoutInflater();
				View mView = inflater.inflate(
						R.layout.dialog_delete_replyorcomment, null);
				TextView delete = (TextView) mView.findViewById(R.id.delete);
				TextView cancel = (TextView) mView.findViewById(R.id.cancel);
				// LayoutParams params=new
				// LayoutParams(getActivity().getWindowManager().getDefaultDisplay().getWidth(),100);
				// mView.setLayoutParams(params);
				customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				customDialog.setContentView(mView);
				customDialog.setCanceledOnTouchOutside(true);
				customDialog.setCancelable(true);
				setFinishOnTouchOutside(true);
				customDialog.show();
				delete.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						presenter.deleteComment(uid,
								model.getReplyInfo().getList().get(position)
										.getComments().get(chidPosition)
										.getTid(), model.getReplyInfo()
										.getList().get(position).getComments()
										.get(chidPosition).getPid(), model
										.getReplyInfo().getList().get(position)
										.getComments().get(chidPosition)
										.getId());
						if (model.getReplyInfo() == null) {
							view2.setVisibility(View.GONE);
							huifu_tv.setVisibility(View.GONE);
						}
						customDialog.dismiss();
					}
				});
				cancel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						customDialog.dismiss();
					}
				});
			} else {

			}
		}
	};

	OnClickListener viewMoreClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			position = Integer.parseInt(v.getTag().toString());
			// TODO Auto-generated method stub
			Intent intent = new Intent(MainApplication.context,
					PingLunAndHuiFuDetailActivity.class);
			intent.putExtra("pid", model.getReplyInfo().getList().get(position)
					.getPid());
			intent.putExtra("tid", model.getReplyInfo().getList().get(position)
					.getTid());
			intent.putExtra("fid", model.getReplyInfo().getList().get(position)
					.getFid());
			startActivity(intent);
		}
	};

	@SuppressWarnings("deprecation")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity info = handlerError(data);
		if (info != null) {
			// ByAlert.alert("" + info.getData());
			if (info.getEventType() == TieZiPresenter.XiangQingSuccess) {
				model = (GetAList) info.getData();
				toUid = model.getAuthorId();
				tid = model.getTid();
				// aLists.addAll(((TieZiBaoHanReply) info.getData()).getList());
				question_show.setText(model.getMessage());
				author_name.setText(model.getAuthor());
				dianzan_num.setText(model.getLikes());
				pinglun_num.setText(model.getReplyInfo().getCount());
				if (model.getBabyDefault() != null
						&& model.getBabyDefault().getBirthday() != null) {
					baby_sex.setVisibility(View.VISIBLE);
					baby_years.setVisibility(View.VISIBLE);
					baby_sex.setText(babysex(model.getBabyDefault().getSex()));
					baby_years.setText(babybirth(model.getBabyDefault()
							.getBirthday()));

				} else {
					baby_sex.setVisibility(View.GONE);
					baby_years.setVisibility(View.GONE);
				}
				guanzhu.setText("删除");
				guanzhu.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						showProgressDialog("删除中，请稍后" + tid, true);
						LuntanTieZiBean model = new LuntanTieZiBean();
						model.setTid(tid);
						model.setUid(uid);
						presenter.delete(model);
						ByAlert.alert("删除成功");
						Intent intent = new Intent(MyTieZiDetailActivity.this,
								MyTieZiActivity.class);
						startActivity(intent);
						finish();
					}
				});
				IScollection = model.getIsFav();
				guanzhu.setTag(R.id.guanzhu, model.getIsFav());
				Log.e("hu--shoucang", model.getIsFav());
				Log.d("hu", model.getReplyInfo().getCount());
				if (!model.getReplyInfo().getCount().equals("0")
						|| model.getReplyInfo().getList() != null) {
					huifu_tv.setVisibility(View.VISIBLE);
					view2.setVisibility(View.VISIBLE);
					if (toUid.equals(uid)) {

					}
					YuErJingCommentDetailAdapter adapter = new YuErJingCommentDetailAdapter(
							this, model.getReplyInfo().getList(),
							Integer.parseInt(model.getReplyInfo().getCount()),
							clickListener, huiFuPingLunOnclickListener,
							viewMoreClickListener);
					list_wodedongtaidetail.setAdapter(adapter);
					// adapter.notifyDataSetChanged();
				}
				if (model.getReplyInfo().getCount().equals("0")
						|| model.getReplyInfo().getList() == null) {
					huifu_tv.setVisibility(View.GONE);
					view2.setVisibility(View.GONE);

				}
				if (Integer.parseInt(model.getIsLike()) == 0) {
					img_dianzan.setImageResource(R.drawable.like_unselect);
				} else {
					img_dianzan.setImageResource(R.drawable.xin_pink);
				}
				img_dianzan.setTag(model.getIsLike());
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ model.getHead(), author_headewrshow);
				img_other.setVisibility(View.VISIBLE);
				image_container.removeAllViews();
				if (model.getImg() != null) {
					if (model.getImg().size() > 0) {
						new MyRunnable(this, model.getImg(), null, model
								.getImg().size(), screenWidth, displayMetrics,
								image_container);
					} else {
						image_container.setVisibility(View.GONE);
					}

				} else {
					image_container.setVisibility(View.GONE);
				}
				edt_comments.setText("");
				edt_comments.setHint("我也说一句");
				System.out.println("replyinfo" + model.getReplyInfo());
				System.out.println("count" + model.getReplyInfo().getCount());
			} else if (info.getEventType() == TieZiPresenter.XiangQingFail) {
				// ByAlert.alert("查询失败" + info.getData());
			} else if (info.getEventType() == TieZiPresenter.PinglunSuccess) {
				presenter.zhutiXiangqing(uid, tid, "3");
				edt_comments.setText("");
				((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(MyTieZiDetailActivity.this
								.getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
			} else if (info.getEventType() == TieZiPresenter.PingLunFail) {
				// ByAlert.alert("查询失败" + info.getData());
			} else if (info.getEventType() == TieZiPresenter.DeleteCommentSuccess) {
				// ByAlert.alert("删除成功");
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.DeleteCommentFail) {
				// ByAlert.alert("删除失败");
			} else if (info.getEventType() == TieZiPresenter.PinglunSuccess) {
				// ByAlert.alert("评论成功" );
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.PingLunFail) {
				// ByAlert.alert("评论失败");
			} else if (info.getEventType() == TieZiPresenter.huiFuPinglunSuccess) {
				// ByAlert.alert("回复成功" );
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.huiFuPingLunFail) {
				// ByAlert.alert("回复失败" );
			} else if (info.getEventType() == TieZiPresenter.DeletePingLunHuiFuSuccess) {
				// ByAlert.alert("删除成功" );
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.DeletePingLunHuiFail) {
				// ByAlert.alert("删除失败" );
			} else if (info.getEventType() == TieZiPresenter.GuanzhuSuccess) {
				// ByAlert.alert( + "");
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.GuanzhuFail) {
				// ByAlert.alert(info.getData() + "");
			} else if (info.getEventType() == TieZiPresenter.CollectionTieziSuccess) {
				// ByAlert.alert(info.getData() + "");
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.CollectionTieziFail) {
				// ByAlert.alert(info.getData() + "");
			} else if (info.getEventType() == TieZiPresenter.DianzanSuccess) {
				// ByAlert.alert(info.getData() + "");
				presenter.zhutiXiangqing(uid, tid, "3");
			} else if (info.getEventType() == TieZiPresenter.DianzanyeFail) {
				// ByAlert.alert(info.getData() + "");
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	int screenWidth, screenHeight;
	int totalSize = 0;
	private Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Bitmap bitmap = null;

			screenWidth = getWindowManager().getDefaultDisplay().getWidth();
			bitmap = ImageLoader.getInstance().loadImageSync(
					GlobalConfig.API_URL + "/picture/index?id="
							+ model.getImg().get(totalSize));

			if (bitmap != null) {
				Message msg = new Message();
				msg.obj = bitmap;
				handler.sendMessage(msg);
			}
		}
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			// if (model.getImg().size() != 0) {
			// for (int i = 0; i < model.getImg().size(); i++) {
			ImageView imageView = new ImageView(MyTieZiDetailActivity.this);
			LayoutParams layoutParams = new LayoutParams(screenWidth,
					LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(2, 4, 2, 0);
			imageView.setLayoutParams(layoutParams);
			setImageViewMathParent((Bitmap) msg.obj, imageView);

			// XImageLoader.load(GlobalConfig.API_URL
			// + "/picture/index?id="
			// + model.getImg().get(i), imageView);
			image_container.addView(imageView);
			// }
			// } else {
			// image_container.setVisibility(View.GONE);
			// }
			if (totalSize < model.getImg().size() - 1) {
				handler.post(runnable);
				totalSize++;
				System.out.println("tota" + totalSize);
			} else {
				handler.removeCallbacks(runnable);
			}
		};
	};

	public void setImageViewMathParent(Bitmap bitmap, ImageView image) {
		if (bitmap != null) {

			float scalew = (float) displayMetrics.widthPixels
					/ (float) bitmap.getWidth();
			image.setScaleType(ScaleType.FIT_XY);
			Matrix matrix = new Matrix();
			image.setAdjustViewBounds(true);
			if (displayMetrics.widthPixels < bitmap.getWidth()) {
				matrix.postScale(scalew, scalew);
			} else {
				matrix.postScale(1 / scalew, 1 / scalew);
			}
			image.setMaxWidth(displayMetrics.widthPixels);
			float ss = displayMetrics.heightPixels > bitmap.getHeight() ? displayMetrics.heightPixels
					: bitmap.getHeight();
			image.setMaxWidth((int) ss);
			image.setImageBitmap(bitmap);
			if (bitmap != null && bitmap.isRecycled()) {
				bitmap.recycle();
			}
		}

	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			type = "回复";
			edt_comments.setHint("评论");
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.showSoftInput(edt_comments, 0);
		}
	};

	private String babysex(String sex) {
		if ("0".equals(sex)) {
			babysex = "女宝";
		} else if ("1".equals(sex)) {
			babysex = "男宝";

		} else {
			babysex = "孕育中";
		}

		return babysex;

	}

	private String babybirth(String birth) {

		SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日");
		Calendar calendar = Calendar.getInstance();
		int yearNow = calendar.get(Calendar.YEAR);
		int monthNow = calendar.get(Calendar.MONTH);
		int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----", yearNow + "monthNow-----" + "-------dayBirth"
				+ monthNow + "n" + dayNow);
		Calendar calendar2 = Calendar.getInstance();
		long timews = Long.valueOf(birth);
		Date date = new Date(timews * 1000L);
		calendar2.setTime(date);
		// Log.d("hu", date.toString());
		int yearBirth = calendar2.get(Calendar.YEAR);
		int monthBirth = calendar2.get(Calendar.MONTH);
		int dayBirth = calendar2.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----2", yearBirth + "monthBirth-----" + "-------dayBirth"
				+ monthBirth + "n" + dayBirth);
		int age = yearNow - yearBirth;
		int month = monthNow - monthBirth;
		int day = dayNow - dayBirth;
		if (age > 0) {
			if (month > 0) {
				if (day < 0) {
					month--;
					age--;
				}

			} else if (month < 0) {
				month += 12;
				if (day < 0) {
					month--;
					age--;
				}
			}

		} else {
			if (day < 0) {
				month--;
			}

		}
		SimpleDateFormat myFmt5 = new SimpleDateFormat("d");
		String birthday2 = myFmt1.format(Long.parseLong(birth) * 1000L);
		String birthday = myFmt5.format(System.currentTimeMillis()
				- Long.parseLong((birth) + "") * 1000L);
		int days = Integer.parseInt(birthday) % 30;
		Log.d("hu-bir", birthday2);
		Log.d("hu-old", (age + "岁" + month + "月" + days + "天"));
		if (day == 0) {
			babyyears = age + "岁" + month + "月" + day + "天";
		} else {
			babyyears = age + "岁" + month + "月" + days + "天";
		}

		return babyyears;
	}
}
